import Vue from 'vue'
//引入vue-router
import VueRouter from 'vue-router'
import { getToken } from '../utils/localData'
Vue.use(VueRouter)
//登录页面
import Login from '../views/Login.vue'
//注册页面
import Register from '../views/Register.vue'
//个人中心
import User from '../views/User.vue'
import UserEdit from '../views/UserEdit.vue'
import Home from '../views/Home.vue'
import articleDetail from'../views/articleDetail.vue'
import UserFollow from'../views/UserFollow.vue'
import UserStar from'../views/UserStar.vue'
import UserComment from'../views/UserComment.vue'
import Comment from  '../views/Comment.vue'
import Category from '../views/Category.vue'
import Search from '../views/Search.vue'
const routes=[
    {path:'/',component:Home},
    {path:'/login',component:Login},
    {path:'/register',component:Register},
    {path:'/user',component:User},
    {path:'/user_edit',component:UserEdit},
    {path:'/articleDetail:id',component:articleDetail},
    {path:'/user_follow',component:UserFollow},
    {path:'/user_comment',component:UserComment},
    {path:'/user_star',component:UserStar},
    {path:'/comment/:id',component:Comment},
    { path: '/category', component: Category },
    { path: '/search', component: Search },
]
const router = new VueRouter({
    routes,
    //去除网页地址#号
    mode:'history'
})
// to  去哪里
//from  跳转前地址，从哪里来
//next  下一个函数，是否放行
router.beforeEach((to,from,next)=>{
    //定义一个数组，在这个数组范围内路径，都要进行登录效验
    const pathArr = ['/user', '/user_edit']
    //判断部分页面访问的时候有没有token
if(pathArr.indexOf(to.path) !== -1){
    //如果有成功获取到token
if(getToken()){
    //放行
    next()
}else{
    //没有就跳转到登录页面
    next({path:'/login'})
}
}else{
    //普通页面直接放行
    next()
}
})
export default router